/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package net.risesoft.risebi.collection.web.client;

import net.risesoft.risebi.collection.model.User;
import net.risesoft.risebi.collection.service.UserManager;
import net.risesoft.risebi.collection.web.BaseAction;
import net.sf.json.JSONObject;

import org.apache.commons.beanutils.LazyDynaBean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author <a href="mailto: dingzhaojun@risesoft.net">丁兆俊</a>
 * @version $Revision$
 * 
 */

@Namespace("/client/UserAction")
public class UserAction extends BaseAction{	
	private static final long serialVersionUID = -6803089603340474477L;
	private static Log log = LogFactory.getLog(UserAction.class);
	
	private UserManager userManager;
	private User user;

	@Action(value="prepareEditUserPassword",
			results={@Result(name="success", location="/client/user/editUserPassword.jsp")}
	)
	public String prepareEditUserPassword() throws Exception {
		user = (User)userManager.getObject(User.class, this.getSessionUser().getGuid());
		
		return SUCCESS;
	}
	
	@Action(value="editUserPassword")
	public String editUserPassword() throws Exception {
		LazyDynaBean ldb = new  LazyDynaBean();
		try{
			User oldUser = (User)userManager.getObject(User.class, user.getGuid());
		
			if(!oldUser.getPassword().equals(user.getPassword())){
				ldb.set("success", false);
				ldb.set("reason", "输入的用户旧密码不正确！");
				this.json = JSONObject.fromObject(ldb).toString();
				return AJAX;
			}
			
			if(node.equals(user.getInitialPassword())){
				ldb.set("success", false);
				ldb.set("reason", "密码过于简单，请重新输入新密码！");
				this.json = JSONObject.fromObject(ldb).toString();
				return AJAX;
			}
			
			oldUser.setPassword(this.node);
			userManager.saveObject(oldUser);
			
			ldb.set("success", true);
			ldb.set("reason", "修改用户密码成功！");			
			this.json = JSONObject.fromObject(ldb).toString();
		}catch(Exception e){
			ldb.set("success", false);
			ldb.set("reason", e.getMessage());
			this.json = JSONObject.fromObject(ldb).toString();
		}

		return AJAX;
	}

	public UserManager getUserManager() {
		return userManager;
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}
